<template>
  <div class="login-container">
    <div class="login-content" ref="loginContent">
      <div class="login-logo">
        <h2>视频解析系统</h2>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on">
        <div class="title-container">
          <h3 class="title">欢迎登录</h3>
        </div>

        <!-- 用户名 -->
        <el-form-item prop="username" class="form-item">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            type="text"
            tabindex="1"
            autocomplete="on"
            class="custom-input"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password" class="form-item">
          <el-input
            v-model="loginForm.password"
            :type="passwordVisible ? 'text' : 'password'"
            placeholder="请输入密码"
            tabindex="2"
            autocomplete="on"
            @keyup.enter="handleLogin"
            class="custom-input"
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
            <template #suffix>
              <el-icon class="input-icon-clickable" @click="passwordVisible = !passwordVisible">
                <View v-if="passwordVisible" />
                <Hide v-else />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="captcha" class="form-item">
          <div class="captcha-container">
            <el-input
              v-model="loginForm.captcha"
              placeholder="请输入验证码"
              type="text"
              tabindex="3"
              autocomplete="off"
              @keyup.enter="handleLogin"
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Picture /></el-icon>
              </template>
            </el-input>
            <div class="captcha-img" @click="refreshCaptcha">
              <img :src="captchaUrl" alt="验证码" />
            </div>
          </div>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-button 
          :loading="loading" 
          type="primary" 
          class="login-button" 
          @click="handleLogin"
        >
          <span>登 录</span>
        </el-button>

        <!-- 注册链接 -->
        <div class="register-link">
          <span>还没有账号？</span>
          <router-link to="/register">立即注册</router-link>
          
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import gsap from 'gsap'

const store = useStore()
const route = useRoute()
const router = useRouter()

// 表单引用
const loginFormRef = ref(null)
const loginContent = ref(null)

// 加载状态
const loading = ref(false)

// 密码可见状态
const passwordVisible = ref(false)

// 验证码相关
const captchaUrl = ref('')
const captchaKey = ref('')

// 登录表单数据
const loginForm = reactive({
  username: 'admin',
  password: '123456',
  captcha: '',
  captchaKey: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 4, message: '验证码长度为 4 个字符', trigger: 'blur' }
  ]
}

// 刷新验证码
const refreshCaptcha = () => {
  // 生成随机验证码key
  captchaKey.value = new Date().getTime().toString()
  loginForm.captchaKey = captchaKey.value
  // 设置验证码URL
  captchaUrl.value = `http://localhost:8080/api/auth/captcha?key=${captchaKey.value}&t=${new Date().getTime()}`
}

// 处理登录
const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (valid) {
      loading.value = true
      try {
        await store.dispatch('user/login', loginForm)
        // 登录成功动画
        gsap.to(loginContent.value, {
          duration: 0.5,
          opacity: 0,
          y: -50,
          ease: 'power2.in',
          onComplete: () => {
            // 获取重定向地址
            const redirect = route.query.redirect || '/'
            // const redirect = '/dashboard';
            router.push(redirect)
            ElMessage.success('登录成功')
          }
        })
      } catch (error) {
        console.error('登录失败:', error)
        // 刷新验证码
        refreshCaptcha()
        // 登录失败抖动动画
        gsap.to(loginContent.value, {
          duration: 0.1,
          x: -10,
          ease: 'power1.inOut',
          repeat: 3,
          yoyo: true,
          onComplete: () => {
            gsap.to(loginContent.value, {
              duration: 0.1,
              x: 0
            })
          }
        })
      } finally {
        loading.value = false
      }
    } else {
      return false
    }
  })
}

// 组件挂载时刷新验证码和添加入场动画
onMounted(() => {
  refreshCaptcha()
  
  // 入场动画
  gsap.from(loginContent.value, {
    duration: 0.8,
    opacity: 0,
    y: 30,
    ease: 'power3.out'
  })
})
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('') repeat;
    opacity: 0.3;
  }

  .login-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 440px;
  }

  .login-logo {
    margin-bottom: 20px;
    
    h2 {
      color: white;
      font-size: 32px;
      text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
      letter-spacing: 1px;
      font-weight: 600;
    }
  }

  .login-form {
    position: relative;
    width: 100%;
    padding: 40px;
    margin: 0 auto;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    }
  }

  .title-container {
    position: relative;
    margin-bottom: 30px;

    .title {
      font-size: 28px;
      color: #333;
      margin: 0 auto 20px auto;
      text-align: center;
      font-weight: bold;
      letter-spacing: 1px;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -10px;
        left: 50%;
        transform: translateX(-50%);
        width: 50px;
        height: 3px;
        background: linear-gradient(90deg, #667eea, #764ba2);
        border-radius: 3px;
      }
    }
  }

  .form-item {
    margin-bottom: 25px;
  }

  .custom-input {
    :deep(.el-input__wrapper) {
      padding: 1px 15px;
      box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
      transition: all 0.3s ease;
      background: rgba(255, 255, 255, 0.8);
      
      &:hover, &.is-focus {
        box-shadow: 0 0 0 1px #667eea;
      }
    }
    
    :deep(.el-input__inner) {
      height: 45px;
      font-size: 15px;
    }
  }

  .input-icon {
    color: #667eea;
    font-size: 18px;
    margin-right: 8px;
  }

  .input-icon-clickable {
    color: #667eea;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
      color: #764ba2;
      transform: scale(1.1);
    }
  }

  .captcha-container {
    display: flex;
    align-items: center;

    .el-input {
      flex: 1;
    }

    .captcha-img {
      margin-left: 10px;
      height: 45px;
      cursor: pointer;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
      }

      img {
        height: 100%;
      }
    }
  }

  .login-button {
    width: 100%;
    margin-bottom: 30px;
    height: 50px;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 2px;
    background: linear-gradient(90deg, #667eea, #764ba2);
    border: none;
    border-radius: 8px;
    transition: all 0.3s ease;
    box-shadow: 0 4px 10px rgba(102, 126, 234, 0.4);
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 15px rgba(102, 126, 234, 0.5);
      background: linear-gradient(90deg, #5a71d6, #6a43a0);
    }
    
    &:active {
      transform: translateY(0);
      box-shadow: 0 2px 8px rgba(102, 126, 234, 0.4);
    }
  }

  .register-link {
    text-align: center;
    font-size: 14px;
    color: #666;
    margin-top: 20px;

    a {
      color: #667eea;
      text-decoration: none;
      font-weight: 600;
      transition: all 0.3s ease;

      &:hover {
        color: #764ba2;
        text-decoration: none;
      }
    }
  }
}
</style>